﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;

namespace MyWeb.Common
{
    public class ControlClass : System.Web.UI.UserControl
    {
        public static void SetPostBackUrlLinkControl(Control parent) 
        {
            foreach (Control c in parent.Controls)
            {
                string abc = c.ID;
                if (c.Controls.Count > 0)
                {
                    SetPostBackUrlLinkControl(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.LinkButton":
                            ((LinkButton)c).PostBackUrl = c.ID.Replace("lbt", "/Admin/") + ".aspx";
                            break;
                    }
                }
            }
        }

        public static void SetPermistionControlForUser(Control parent, string permission)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    SetPermistionControlForUser(c, permission);
                }
                else
                {
                    string cType = c.GetType().ToString();
                    bool disable_control = false;
                    if (cType.Equals("System.Web.UI.WebControls.CheckBox") || cType.Equals("System.Web.UI.WebControls.LinkButton") || cType.Equals("System.Web.UI.WebControls.ImageButton"))
                        switch (cType)
                        {
                            case "System.Web.UI.WebControls.CheckBox":
                                if (((CheckBox)c).ID.Contains("chkSelect"))
                                {
                                    if (!permission.Contains("2"))
                                        disable_control =  true;
                                }
                                if (((CheckBox)c).ID.Contains("chkActive"))
                                {
                                    if (!permission.Contains("3"))
                                        disable_control = true;
                                }
                                if (disable_control)
                                {
                                    ((CheckBox)c).Checked = false;
                                    ((CheckBox)c).Enabled = false;
                                }
                                break;
                            case "System.Web.UI.WebControls.ImageButton":
                                if (permission.Contains("0"))
                                {
                                    if (((ImageButton)c).ID.Contains("cmdAdd") || ((ImageButton)c).ID.Contains("cmdEdit") || ((ImageButton)c).ID.Contains("cmdDelete") || ((ImageButton)c).ID.Contains("cmdActive"))
                                    {
                                        ((ImageButton)c).Enabled = false;
                                        ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("add.png", "add_no.png");
                                        ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("edit.png", "edit_no.png");
                                        ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("delete.png", "delete_no.png");
                                        ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("start.png", "start_no.png");
                                        ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("stop.png", "stop_no.png");
                                    }
                                }
                                else
                                {
                                    if (((ImageButton)c).ID.Contains("cmdActive"))
                                    {
                                        if (!permission.Contains("3"))
                                        {
                                            ((ImageButton)c).Enabled = false;
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("start.png", "start_no.png");
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("stop.png", "stop_no.png");
                                        }
                                    }
                                    if (((ImageButton)c).ID.Contains("cmdAdd") || ((ImageButton)c).ID.Contains("cmdDelete"))
                                    {
                                        if (!permission.Contains("2"))
                                        {
                                            ((ImageButton)c).Enabled = false;
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("add.png", "add_no.png");
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("edit.png", "edit_no.png");
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("delete.png", "delete_no.png");
                                        }
                                    }
                                    if (((ImageButton)c).ID.Contains("cmdEdit"))
                                    {
                                        if (!permission.Contains("1") && !permission.Contains("3"))
                                        {
                                            ((ImageButton)c).Enabled = false;
                                            ((ImageButton)c).ImageUrl = ((ImageButton)c).ImageUrl.Replace("edit.png", "edit_no.png");
                                        }
                                    }
                                }
                                break;
                            case "System.Web.UI.WebControls.LinkButton":
                                if (!permission.Contains("2"))
                                {
                                    disable_control = ((LinkButton)c).ID.Contains("lbtAdd") || ((LinkButton)c).ID.Contains("lbtDelete") || ((LinkButton)c).ID.Contains("lbtUpdate") ? true : false;
                                }
                                if (disable_control){
                                    ((LinkButton)c).Attributes.Remove("onClick");
                                    ((LinkButton)c).Enabled = false;
                                }
                                break;
                        }
                }
            }
        }

        public static void ResetControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                string abc = c.ID;
                if (c.Controls.Count > 0)
                {
                    if (c.GetType().ToString() == "FredCK.FCKeditorV2.FCKeditor") {
                        ((FCKeditor)c).Value = "";
                        break;
                    }
                    else {
                        ResetControlValues(c);
                    }
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.DropDownList":
                            if (((DropDownList)c).Items.Contains(new ListItem("")))
                                ((DropDownList)c).SelectedValue = "";
                            break;  
                        case "System.Web.UI.WebControls.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.HiddenField":
                            ((HiddenField)c).Value = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = ((CheckBox)c).ID.ToLower().Contains("actite") ? true : false;
                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            ((RadioButton)c).Checked = false;
                            break;
                        case "System.Web.UI.WebControls.Image":
                            ((Image)c).ImageUrl = null;
                            ((Image)c).Width = 0;
                            break;
                        case "FredCK.FCKeditorV2.FCKeditor":
                            ((FCKeditor)c).Value = "";
                            break;
                    }
                }
            }
        }
    }
}
